using System;
using System.IO;

namespace XLib
{
    public class ApkZipStream : Stream
    {
        private Stream stream;

        public ApkZipStream(Stream stream)
        {
            this.stream = stream;
        }

        public override int ReadByte()
        {
            return stream.ReadByte();
        }

        public override int Read(byte[] buffer, int offset, int count)
        {
            return stream.Read(buffer, offset, count);
        }

        public override void Write(byte[] buffer, int offset, int count)
        {
            throw new NotSupportedException();
        }

        public override void SetLength(long value)
        {
            throw new NotSupportedException();
        }

        public override long Seek(long offset, SeekOrigin origin)
        {
            stream.Seek(offset, origin);
            return stream.Position; // The AssetBundle data must start at stream position zero. https://docs.unity3d.com/ScriptReference/AssetBundle.LoadFromStream.html
        }

        public override void Flush()
        {
            // Nothing to do.
        }

        public override long Position
        {
            get
            {
                return stream.Position;
            }

            set
            {
                stream.Position = value;
            }
        }

        public override long Length
        {
            get { return stream.Length; }
        }

        public override bool CanWrite
        {
            get { return stream.CanWrite; }
        }

        public override bool CanSeek
        {
            get { return stream.CanSeek; }
        }

        public override bool CanRead
        {
            get { return stream.CanRead; }
        }

        protected override void Dispose(bool disposing)
        {
            stream.Dispose();
            base.Dispose(disposing);
        }
    }
}